package com.ljc.config;

import com.ljc.gateway.InterfaceFilter;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
public class GatewayConfig {

    @Resource
    private InterfaceFilter interfaceFilter;

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                // 配置接口调用的路由（需要经过interfaceFilter校验权限、ak、sk、判断是否有调用次数）
                .route("api-interface", r -> r.path("/api/interface/**")
                        .filters(f -> f.filter(interfaceFilter))
                        .uri("http://localhost:8001"))
                // 第三方服务【登录、支付】
                .route("api-third", r -> r.path("/api/third/**")
                        .uri("http://localhost:8004"))
                // 下单接口次数
                .route("api-order", r -> r.path("/api/order/**")
                        .uri("http://localhost:8003"))
                // 后端系统调用的路由
                .route("api-backend", r -> r.path("/api/**")
                        .uri("http://localhost:8321"))
                .build();
    }
}
